<?php
 /*
  * @author Luong Trong Hieu
  *
 */
?>

<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class AccountController extends Zend_Controller_Action {
    function indexAction()
    {
        $this->view->title="User List";
        $page = $this->_request->getParam('page', 1);
        $offset = 5;
        $acc = new AccountDAO();
        $user = $acc->getUserPage($page, $offset);
        $this->view->data = $user;
    }

    function infoAction()
    {
        $this->view->title="Account Info";
        $id = $this->_request->getParam('id', -1);
        if($id > 0)
        {
            $acc = new AccountDAO();
            $accInfo = $acc->getUser($id);
            if($accInfo)
            {
                $this->view->id = $accInfo['id'];
                $this->view->username = $accInfo['username'];
                $this->view->fullname = $accInfo['fullname'];
                $this->view->byear = $accInfo['b_year'];
                $this->view->sex = $accInfo['sex'];
                $this->view->email = $accInfo['email'];
                $this->view->address = $accInfo['address'];
                $this->view->status = $accInfo['status'];
            }
            else
            {
                $this->view->message = "User not found!<br />";
            }
        }
		else
			$this->view->message = "User not found! <br />";
    }

    function addAction()
    {
        $this->view->title="Add user";
        if($this->_request->isPost())
        {
            $account = new AccountDAO();
            $password = md5($_POST['password']);
            
            $data = array(
                'username' => $_POST['username'],
                'password' => $password,
                'fullname' => $_POST['fullname'],
                'b_year' => $_POST['byear'],
                'sex' => $_POST['sex'],
                'email' => $_POST['email'],
                'address' => $_POST['address'],
                'role_id' => Constant::$ROLE_USER,
                'type_id' => $_POST['typeid'],
                'status' => Constant::$STATUS_AVAILABLE
                );

            if($account->insertUser($data))
                {
                    $this->view->message = "User has been added!<br />";
                    $this->view->success = 1;
                }
            else
            {
                $this->view->success = 0;
                $this->view->message = "Username or email exist!<br />";
            }
        }
    }
}
?>
